package com.google.code.tretris;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import com.google.code.tretris.server.GamePlayService;
import com.google.code.tretris.server.TretrisServer;

public class ServerConsoleActivity extends Activity {

	private static final String LOG_TAG = ServerConsoleActivity.class
			.getSimpleName();
	private ServiceConnection serviceConnection;
	private TretrisServer serverInstance = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.serverconsole);

		final View stopButton = findViewById(R.id.StopGameplayButton);
		stopButton.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				if (serviceConnection != null) {
					Log.d(LOG_TAG, "Unbinding from service");
					unbindService(serviceConnection);
					serviceConnection = null;
				}

				Intent intent = new Intent(getApplicationContext(),
						GamePlayService.class);
				Log.d(LOG_TAG, "Intenting service to stop");
				stopService(intent);
				serverInstance = null;
				refreshGUI();
			}
		});

		serviceConnection = new ServiceConnection() {
			public void onServiceDisconnected(ComponentName name) {
				Log.d(LOG_TAG, "Service disconnected: "
						+ name.flattenToString());
				serverInstance = null;
				refreshGUI();
			}

			public void onServiceConnected(ComponentName name, IBinder service) {
				Log.d(LOG_TAG, "Service connected: " + name.flattenToString());
				serverInstance = TretrisServer.Stub.asInterface(service);
				refreshGUI();
			}
		};

		Intent serviceIntent = new Intent(this, GamePlayService.class);
		boolean boundToService = bindService(serviceIntent, serviceConnection,
				BIND_AUTO_CREATE);
		if (boundToService) {
			Log.d(LOG_TAG, "Binding to service");
		} else {
			Log.w(LOG_TAG, "Unable to bind to service");
		}

	}

	private void refreshGUI() {
		setLabelBooleanValue(R.id.service_created_value, (serverInstance != null));
		try {
			if (serverInstance != null) {
				setLabelBooleanValue(R.id.service_started_value, serverInstance
						.isStarted());
			} else {
				clearLabelValue(R.id.service_started_value);
			}
			final View stopButton = findViewById(R.id.StopGameplayButton);
			if ((serverInstance != null) && (serverInstance.isStarted())) {
				stopButton.setEnabled(true);
			} else {
				stopButton.setEnabled(false);
			}

			if (serverInstance != null) {
				TextView view = (TextView) findViewById(R.id.number_of_players_value);
				view.setText("" + serverInstance.getNumberOfPlayers()) ;
			} else {
				clearLabelValue(R.id.number_of_players_value);
			}
			
			if (serverInstance != null) {
				setLabelBooleanValue(R.id.play_in_progress_value,
						serverInstance.isPlayInProgress());
			} else {
				clearLabelValue(R.id.play_in_progress_value);
			}
			if (serverInstance != null) {
				setLabelBooleanValue(R.id.connected_value, serverInstance
						.isLoggedIntoNetwork());
			} else {
				clearLabelValue(R.id.connected_value);
			}
		} catch (RemoteException e) {
			Log.e(LOG_TAG, "Service seems to have crashed", e);
		}
	}

	private void clearLabelValue(int viewID) {
		TextView view = (TextView) findViewById(viewID);
		view.setText("-");
	}

	private void setLabelBooleanValue(int viewID, boolean value) {
		TextView view = (TextView) findViewById(viewID);
		if (value) {
			view.setText(R.string.generic_value_yes);
		} else {
			view.setText(R.string.generic_value_no);
		}
	}

	@Override
	protected void onDestroy() {
		if (serviceConnection != null) {
			unbindService(serviceConnection);
			serviceConnection = null;
		}
		super.onDestroy();
	}

}
